#include <stdio.h>
char ch='y';
int i;
int k=0;
int m;
int choice=1;
int o=0;
int size= 0;
struct node{
int value;
struct node * next;
};
struct node * first =NULL;

void * append(struct node *first)
{
char ch ='y';
struct node *newptr;

while(ch=='y' || ch =='Y')
{
printf("\n Enter the  element");
scanf("%d",&m);
struct node * newptr;
newptr =malloc(sizeof(struct node));
newptr->value=m;
newptr ->next =first;
first =newptr;
printf("\n Enter 'Y ' or 'y ' to continue inserting elements in list ");
scanf("%s",& ch);

}
return first;
}
void * display(struct node * first)
{ 
struct node * p;
for (p=first;p!=NULL;p=p->next)
{
printf("%d \n",p->value);
}
return first;
}

void * enter(struct node * first)
{
while(ch=='y' || ch== 'Y')

{

printf("\n Enter the  element");
scanf("%d",&m);
struct node * newptr;
newptr =malloc(sizeof(struct node));
newptr->value=m;
newptr ->next =first;
first =newptr;
printf("\n Enter 'Y ' or 'y ' to continue inserting elements in list ");
scanf("%s",& ch);
 

}
return first;
}

void * reverse (struct node * first)
{
struct node *p,*t;
struct node * fir =NULL;
struct node * newptr;
for (p=first;p!=NULL;p=t)
{t=p->next;
newptr=p;
newptr->next= fir;
fir= newptr;

}

first= fir;
return first;
}


int  count (struct node * first)
{

struct node *p,*t;

for (p=first;p!=NULL;p=p->next)
{++k;

}

return k;
}

void * insert (struct node * first,int pos,int value)
{int p;
struct node *t=NULL;
struct node *newptr ;
t=first;
for (p=0;p<pos-1;p++)
{t=t->next;}
newptr=malloc(sizeof(struct node));
newptr->value= value;
newptr->next = t->next;
t->next =newptr;



return first;
}



void main()
{
int ans,pos,val;
while (choice !=12)
{
printf("\n Enter your choice");
printf("\n Enter 1 to append");
printf("\n Enter 2 to extend");
printf("\n Enter 3 to insert");   
printf("\n Enter 4 to remove");
printf("\n Enter 5 to pop");
printf("\n Enter 6 to find search or get index");
printf("\n Enter 7 to count");
printf("\n Enter 8 to sort");
printf("\n Enter 9 to reverse");
printf("\n Enter 10 to enter list initially");
printf("\n Enter 11 to display");
printf("\n Enter 12 to exit");


scanf("%d",&choice);


switch(choice)
{
case 1 :first= append(first);
		break;
case 7:  ans = count(first);
printf("No of elements are %d",ans);
break;
case 3 :
printf("Enter position at which insert");
scanf("%d",&pos);
printf("Enter value to insert");
scanf("%d",&val);

 first=insert(first,pos,val);
break;


case 6: 

case 9 : first =reverse (first);
 		break;
case 10:first= enter(first);

		break;

case 11 :first= display(first);
		

		break;
}

}
}
